<?php
namespace cms\controllers;

use Yii;
use cms\controllers\BasicController;
use common\component\exception\ErrMap;
use common\models\OperationLog;
use common\models\Album;
use common\models\AlbumCategory;
use common\component\tool\Tool;
use common\behaviors\P3p;


class AuthController extends BasicController
{
    protected $userInfo;//登录信息
    protected $permissions;//角色权限信息
    protected $permissionApps;//角色权限信息

    public function behaviors()
    {
        return [
             // 注册匿名行为，只有行为类名
            P3p::className(),

        ];
    }

    public function init()
    {
        parent::init();
        /*请求认证中心,获取用户登录信息*/
        // $this->userInfo = 1;
        if(isset(Yii::$app->session->get('loginInfo')->userInfo)){
            $this->userInfo = Yii::$app->session->get('loginInfo')->userInfo;
            $this->permissions = Yii::$app->session->get('loginInfo')->permissions;
            $this->permissionApps = Yii::$app->session->get('loginInfo')->permissionApps;
        };
    }

    /*验证是否登录*/
    public function beforeAction($action)
    {
        $module = $this->module->id;
        $controller = $this->id;
        $action = $this->action->id;
        $router = $module . '=' . $controller . '=' . $action;

        if (parent::beforeAction($action)) {
            if(!empty($this->userInfo)){
                // return true;

                /*如果父控制器BasicController 取得comid跟登录的comid不一样，说明有人恶意调取其他应用的数据*/
                // P($this->userInfo['comid']);
                // P($this->comid);
                if(isset($this->comid) and ($this->comid != $this->userInfo['comid'])){
                    list($errno, $errmsg) = ErrMap::getError(406);//您没有操作此实例的权限
                }else{
                    /*验证权限*/
                    $pass = false;
                    if($this->userInfo['issuper'] == 1 or
                        in_array($router, Yii::$app->params['allowRouter'])){//菜单列表
                        $pass = true;
                    }else{
                        foreach($this->permissions as $k => $v){
                            if(isset($this->get['appid'])){
                                if($v['appid'] == $this->get['appid'] and $v['m'] == $module and $v['c'] == $controller and $v['a'] == $action){
                                    $pass = true;
                                    break;
                                }
                            }else{
                                // echo $v['m'] . '*' . $v['c'] . '*' . $v['a'] . '<br>';
                                // echo $module . '-' . $controller . '-' . $action . '<br>';
                                // echo '----------------<br>';
                                if($v['m'] == $module and $v['c'] == $controller and $v['a'] == $action){
                                    $pass = true;
                                    break;
                                }
                            }
                        }
                    }
                    if($pass){
                        return true;
                    }else{
                        list($errno, $errmsg) = ErrMap::getError(10020);//没有权限
                    }
                }

            };
            if(!isset($errno)){
                list($errno, $errmsg) = ErrMap::getError(10007);
            }
            $args = array(
                'loginid' => 0,
                'loginname' => '',
                'back_code' => $errno,
                'back_info' => $errmsg,
            );
            if(gettype($res = OperationLog::addLog($args)) != 'boolean'){//操作日志
                Yii::$app->end($res);
            };
        }
    }



    /*设置列表页 每页显示多少条*/
    public function actionSetPageSize(){
        if(isset($this->get['pageSize'])){
            $pageSize = $this->get['pageSize'];
            $this->setPageSize($pageSize);
            return Tool::showRes([0, '设置成功']);
        }
    }


    /*验证comid，是否在登录账号所管理的范围内*/
    protected function checkComID($comid)
    {
        if($this->userInfo['comid'] == '00000000' or $this->userInfo['comid'] == $comid){
            return $comid;
        };
        $sql = 'select comid from {{%company}} where comid = :comid and recommend = (
            select com_id from {{%company}} where comid = "'. $this->userInfo['comid'] .'"
            )';
        $company = Company::findBySql($sql, [":comid" => $comid])->one();
        if(!is_null($company)){
            return $company->comid;
        }
        return false;
    }
    

    
}
